package com.itestor.iptv.modules.alert.dao;

import java.sql.CallableStatement;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import org.apache.log4j.Logger;
import org.hibernate.Query;
import org.hibernate.Session;
import com.itestor.iptv.global.dao.GenericHibernateDao;
import com.itestor.iptv.modules.alert.domain.RobotLogDetail;
import com.itestor.iptv.modules.alert.vo.RobotLogDetailBean;
import com.itestor.iptv.utils.JdbcUtil;

/**
 * @author Mr.li
 */
public class TbRobotLogDetailDao extends
        GenericHibernateDao<RobotLogDetail, Long> {

    private static final Logger logger = Logger
            .getLogger(TbRobotLogDetailDao.class);

    /**
     * @param idRobotLog
     * @return boolean
     */
    public boolean alwaysHasDetailsByLogId(Long idRobotLog) {
        String hsql_str = "select count(t.idRobotLogDetail) from RobotLogDetail t where t.idRobotLog=:idRobotLog";
        Query query = getHibernateSession().createQuery(hsql_str);
        query.setParameter("idRobotLog", idRobotLog);

        Long count = (Long) query.uniqueResult();
        return (count > 0) ? true : false;
    }

    /**
     * 获取告警详细信息
     * @param pIdRobotLog
     * @return List
     */
    @SuppressWarnings("unchecked")
    public List<RobotLogDetailBean> getRobotLogDetail(Long pIdRobotLog) {
        StringBuilder hql = new StringBuilder();
        hql
                .append("select new com.itestor.iptv.modules.alert.vo.RobotLogDetailBean(t.logTime,i.irCommand,s.srcIp,s.srcPort,s.destIp,s.destPort"
                        + ",s.rtplost,s.rtpse,s.df,s.mdimlr,s.bitRate,s.portBitRate,s.volume,s.power"
                        + ",e.avFaultDesc,u.userQoe,v.videoPicFile) "
                        + " from RobotLogDetail t,IrRecord i,QosRecord s,QoeRecord e,RobotVideoPic v,UserQoeRecord u "
                        + " where t.idIrRecord=i.idIrRecord and t.idQosRecord=s.idQosRecord and t.idQoeRecord=e.idQoeRecord and t.idRobotVideoPic = v.idRobotVideoPic and t.idUserQoeRecord =u.idUserQoeRecord "
                        + " and t.idRobotLog=:idRobotLog");

        Query query = getHibernateSession().createQuery(hql.toString());
        query.setParameter("idRobotLog", pIdRobotLog);

        return (List<RobotLogDetailBean>) query.list();
    }

    /**
     * 获取告警详细信息
     * @param idRobotLog
     * @return List
     */
    public List<RobotLogDetailBean> getRobotLogDetailListByRobotLogId(
            Long idRobotLog) {
        List<RobotLogDetailBean> resultList = new ArrayList<RobotLogDetailBean>();

        Session session = getHibernateSession();
        Connection conn = session.connection();
        CallableStatement call = null;
        String proc = "{call pr_logmanage_getlogdetail(?)}";
        ResultSet rs = null;
        try {
            call = conn.prepareCall(proc);
            call.setInt(1, idRobotLog.intValue());
            rs = call.executeQuery();

            while (rs.next()) {
                RobotLogDetailBean bean = new RobotLogDetailBean();
                Timestamp time = rs.getTimestamp("LogTime");
                bean.setLogTime(time.getTime());
                bean.setIrCommand(rs.getString("IrCommand"));
                bean.setSrcIp(rs.getString("SrcIp"));
                bean.setSrcPort(rs.getInt("SrcPort"));
                bean.setDestIp(rs.getString("DestIp"));
                bean.setDestPort(rs.getInt("DestPort"));
                bean.setRtplost(rs.getInt("Rtplost"));
                bean.setRtpse(rs.getInt("Rtpse"));
                bean.setDf(rs.getFloat("DF"));
                bean.setMdimlr(rs.getLong("MDIMLR"));
                bean.setBitRate(rs.getLong("BitRate"));
                bean.setPortBitRate(rs.getFloat("PortBitRate"));
                bean.setVolume(rs.getFloat("Volume"));
                bean.setPower(rs.getLong("Power"));
                bean.setAvFaultDesc(rs.getString("AvFaultDesc"));
                bean.setUserQoe(rs.getString("UserQoe"));
                bean.setVideoPicFile(rs.getString("VideoPicFile"));

                resultList.add(bean);
            }

        } catch (SQLException e) {
            logger.error("exec pr_logmanage_getlogdetail err ["
                    + e.getMessage() + "]");
            e.printStackTrace();
        } finally {
            JdbcUtil.close(call, conn);
        }
        return resultList;
    }

    /**
     * 
     * @param pIdRobotLog
     */
    public void execProcedureCollectLog(Long pIdRobotLog) {
        Session session = getHibernateSession();
        Connection conn = session.connection();
        CallableStatement call = null;
        String proc = "{call pr_interior_collectlog(?)}";
        try {
            call = conn.prepareCall(proc);
            call.setInt(1, pIdRobotLog.intValue());
            call.execute();
        } catch (SQLException e) {
            logger
                    .error("execProcedureCollectLog err [" + e.getMessage()
                            + "]");
            e.printStackTrace();
        } finally {
            JdbcUtil.close(call, conn);
        }
    }

    /**
     * 
     * @param pIdRobotLogDetail
     * @return RobotLogDetailBean
     */
    public RobotLogDetailBean getLogDetailBeanByLogDetailId(
            Long pIdRobotLogDetail) {

        StringBuilder hql = new StringBuilder();
        hql
                .append("select new com.itestor.iptv.modules.alert.vo.RobotLogDetailBean(i,v) "
                        + " from RobotLogDetail t,RobotVideoPic v,RobotFileInfo i,RobotLog l"
                        + " where t.idRobotVideoPic=v.idRobotVideoPic and l.idRobot=i.idRobot and t.idRobotLog= l.idRobotLog"
                        + " and t.idRobotLogDetail =:idRobotLogDetail");

        Query query = getHibernateSession().createQuery(hql.toString());
        query.setParameter("idRobotLogDetail", pIdRobotLogDetail);
        return (RobotLogDetailBean) query.uniqueResult();
    }

}